#include "canvas.h"
#include <iostream>
#include <cmath>
using namespace std;

Canvas::Canvas(QWidget *parent) : QGLWidget(parent) {
    setAutoFillBackground(false);
    trackRot = false;
    trackPos = false;
    trackZoom = false;
    zoom.setX(500.0f);
    zoom.setY(500.0f);
}

Canvas::~Canvas() { }

void Canvas::initializeGL() {

}

void Canvas::resizeGL(int width, int height) {
    setupViewport(width, height);
}

void Canvas::tabletEvent(QTabletEvent *event) {
    QPointF p = event->hiResGlobalPos();
    x = p.x();
    y = p.y();
    tx = float(event->xTilt()) / 64.0f;
    ty = float(event->yTilt()) / 64.0f;
    if (event->type() == QEvent::TabletPress) {
        if (event->modifiers() && Qt::AltModifier) {
            if (event->button() == Qt::LeftButton) {
                trackRot = true;
                startRot = p;
            }
            if (event->button() == Qt::MidButton) {
                trackPos = true;
                startPos = p;
            }
            if (event->button() == Qt::RightButton) {
                trackZoom = true;
                startZoom = p;
            }
        }
    }
    if (event->type() == QEvent::TabletRelease) {
        if (event->button() == Qt::LeftButton and trackRot) {
            trackRot = false;
            rot += startRot - p;
        }
        if (event->button() == Qt::MidButton and trackPos) {
            trackPos = false;
            pos += startPos - p;

        }
        if (event->button() == Qt::RightButton and trackZoom) {
            trackZoom = false;
            zoom += startZoom - p;
        }
    }

}

void Canvas::paintEvent(QPaintEvent *event) {
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //setupViewport(width(), height());

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //gluLookAt(sin(x / 100.0f), 2, cos(x / 100.0f), 0, 0, 0, 0, 1, 0);


    float zoomOut = -10.0f;
    if (trackZoom) {
        zoomOut = zoom.x() + startZoom.x() - x + zoom.y() + startZoom.y() - y;
    } else {
        zoomOut = zoom.x() + zoom.y();
    }
    glTranslatef(0.0f, 0.0f, pow(zoomOut / 100.0f, 2.0f) / -20.0f);


    if (trackPos) {
        glTranslatef((pos.x() + startPos.x() - x) / -500.0f, 0.0f, 0.0f);
        glTranslatef(0.0f, (pos.y() + startPos.y() - y) / 500.0f, 0.0f);
    } else {
        glTranslatef(pos.x() / -500.0f, 0.0f, 0.0f);
        glTranslatef(0.0f, pos.y() / 500.0f, 0.0f);
    }

    if (trackRot) {
        glRotatef((rot.y() + startRot.y() - y) / -5.0f, 1.0f, 0.0f, 0.0f);
        glRotatef((rot.x() + startRot.x() - x) / -5.0f, 0.0f, 1.0f, 0.0f);
    } else {
        glRotatef(rot.y() / -5.0f, 1.0f, 0.0f, 0.0f);
        glRotatef(rot.x() / -5.0f, 0.0f, 1.0f, 0.0f);
    }


    glBegin(GL_QUADS);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(-1.0, 1.0, 0.0);
    glVertex3f(1.0, 1.0, 0.0);
    glVertex3f(1.0, -1.0, 0.0);
    glVertex3f(-1.0, -1.0, 0.0);

    glEnd();

    QPainter painter(this);
    update();
}

void Canvas::setupViewport(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30, float(width) / float(height), 0.001, 100.0);
}
